package com.chuangke.system.manage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chuangke.admin.entity.SysRouter;
import com.chuangke.admin.entity.SysUserRole;
import com.chuangke.admin.service.SysUserRoleQueryService;
import com.chuangke.common.constant.SysConfigConstants;
import com.chuangke.common.utils.SecurityUtils;
import com.chuangke.system.entity.SysConfig;
import com.chuangke.system.service.SysConfigService;

import cn.hutool.core.util.StrUtil;

@Component
public class SystemConfigManager {

	@Autowired
	private SysConfigService sysConfigService;
	@Autowired
	private SysUserRoleQueryService sysUserRoleQueryService;

	/**
	 * 查看当前用户是否拥有管理员角色
	 * @return
	 */
	public boolean isAdminRole() {
		SysConfig config = sysConfigService.find("SYSTEM", SysConfigConstants.APP_CINFIG_ADMIN_ROLES);
		if (config == null || StrUtil.isBlank(config.getParamValue())) {
			return false;
		}

		List<String> adminRoleIds = Arrays.asList(config.getParamValue().split(","));

		List<SysUserRole> userRoleList = sysUserRoleQueryService.findByUserId(SecurityUtils.getUserId());

		return !Collections.disjoint(adminRoleIds, userRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList()));
	}
	
	/**
	 * 查看当前用户是否拥有领导角色
	 * @return
	 */
	public boolean isLeaderRole() {
		SysConfig config = sysConfigService.find("SYSTEM", SysConfigConstants.APP_CINFIG_LEADER_ROLES);
		if (config == null || StrUtil.isBlank(config.getParamValue())) {
			return false;
		}

		List<String> adminRoleIds = Arrays.asList(config.getParamValue().split(","));

		List<SysUserRole> userRoleList = sysUserRoleQueryService.findByUserId(SecurityUtils.getUserId());

		return !Collections.disjoint(adminRoleIds, userRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList()));
	}
	
	public List<String> getAppRoleTypeList(){
		List<String> roleTypeList = new ArrayList<>() ;
		List<SysUserRole> userRoleList = sysUserRoleQueryService.findByUserId(SecurityUtils.getUserId());
		
		handleUserAppRoleType(SysConfigConstants.APP_CINFIG_ADMIN_ROLES,userRoleList,roleTypeList) ;
		handleUserAppRoleType(SysConfigConstants.APP_CINFIG_DEPT_ADMIN_ROLES,userRoleList,roleTypeList) ;
		handleUserAppRoleType(SysConfigConstants.APP_CINFIG_NORMAL_ROLES,userRoleList,roleTypeList) ;
		
		return roleTypeList ;
	}
	
	private void handleUserAppRoleType(String appRoleType,List<SysUserRole> userRoleList,List<String> roleTypeList) {
		SysConfig config = sysConfigService.find("SYSTEM", appRoleType);
		if (config == null || StrUtil.isBlank(config.getParamValue())) {
			return ;
		}
		List<String> adminRoleIds = Arrays.asList(config.getParamValue().split(","));
		if(!Collections.disjoint(adminRoleIds, userRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList()))) {
			roleTypeList.add(appRoleType) ;
		}
	}
	
	public SysRouter getRouter() {
		SysConfig config = sysConfigService.find("SYSTEM", "HOME_ROUTER");
		SysRouter sysRouter = new SysRouter();
		sysRouter.setName(config.getName());
		sysRouter.setPath(config == null ? "" : config.getParamValue());
		return sysRouter;
	}
}
